<template>
  <div>
    <el-card>
      <el-alert
        show-icon
        :closable="false"
        title="注意：只允许为三级分类设置属性!!!"
        type="success"
      >
      </el-alert>
      <el-row class="cat">
        <el-col>
          <span class="xuanze">选择商品分类:</span>
          <el-cascader
            v-model="selectKey"
            :options="cateList"
            expand-Trigger="hover"
            :props="listProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-col>
      </el-row>

      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            class="btn"
            type="primary"
            size="mini"
            :disabled="isBtnDis"
            @click="dialogVisible = true"
            >添加参数</el-button
          >
          <!-- 动态参数表格 -->
          <el-table :data="dongtai" border>
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  v-for="(item, i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  class="tagbox"
                  @close="handelclose(scope.row,i)"
                >
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="attr_id" label="#id"></el-table-column>
            <el-table-column
              prop="attr_name"
              label="参数名称"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  class="btn3"
                  type="primary"
                  icon="el-icon-edit"
                  @click="xiugainum(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  class="btn3"
                  type="danger"
                  icon="el-icon-delete"
                  @click="shanchuNum(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态参数" name="only">
          <el-button
            class="btn"
            type="primary"
            size="mini"
            :disabled="isBtnDis"
            @click="dialogVisible = true"
            >添加属性</el-button
          >
          <!-- 静态参数表格 -->
          <el-table :data="jingtai" border>
            <el-table-column type="expand">
                <template slot-scope="scope">
                <el-tag
                  v-for="(item, i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  @close="handelclose(scope.row,i)"
                  class="tagbox"
                  
                >
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="attr_id" label="#id"></el-table-column>
            <el-table-column
              prop="attr_name"
              label="参数名称"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  class="btn3"
                  type="primary"
                  icon="el-icon-edit"
                  @click="xiugainum(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  class="btn3"
                  type="danger"
                  icon="el-icon-delete"
                  @click="shanchuNum(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 添加信息对话框 -->
    <el-dialog
      :title="'添加' + titlText"
      :visible.sync="dialogVisible"
      width="50%"
      @close="tianjiacanshu"
    >
      <el-form
        :model="canshulist"
        status-icon
        :rules="rules"
        ref="canshulist"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="attr_name" :label="titlText">
          <el-input
            type="text"
            class="ipt"
            v-model="canshulist.attr_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcanshu">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改对话框 -->
    <el-dialog
      :title="'修改' + titlText"
      :visible.sync="editdialogVisible"
      width="50%"
      @close="xiugaicanshu"
    >
      <el-form
        :model="editlist"
        status-icon
        :rules="rules"
        ref="editlist"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="attr_name" :label="titlText">
          <el-input
            type="text"
            class="ipt"
            v-model="editlist.attr_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  gettypeList,
  NumList,
  AddNumList,
  DeleteNum,
  gengxinNum,
} from "../../api";
export default {
  name: "GoodsNum",
  data() {
    return {
      rules: {
        attr_name: [
          { required: true, message: "请输入参数名称", trigger: "blur" },
        ],
      },
      canshulist: {
        attr_name: "",
      },
      editlist: {
        attr_name: "",
        attr_id: "",
      },
      editdialogVisible: false,
      dialogVisible: false,
      cateList: [],
      listProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      selectKey: [],
      activeName: "many",
      dongtai: [],
      jingtai: [],
    };
  },
  computed: {
    //禁用按钮
    isBtnDis() {
      if (this.selectKey.length !== 3) {
        return true;
      }
      return false;
    },
    //当前选中的id
    cateId() {
      if (this.selectKey.length == 3) {
        return this.selectKey[2];
      }
      return null;
    },

    //动态计算标题文本
    titlText() {
      if (this.activeName == "many") {
        return "动态参数";
      }
      return "静态参数";
    },
  },

  mounted() {
    gettypeList().then((res) => {
      this.cateList = res.data.data;
    });
  },
  methods: {
    //获取参数列表
    getNumData() {
      if (this.selectKey.length !== 3) {
        this.selectKey = [];
        this.dongtai = [];
        this.jingtai = [];
        return;
      } else {
        //证明选中的是三级
        NumList(this.cateId, this.activeName).then((res) => {
          if (res.data.meta.status == 200) {
            res.data.data.forEach((item) => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
              //控制文本框显示yingcang
              item.inputVisible = false
              item.inputValue = ''
            });
            console.log(res.data.data);
            if (this.activeName == "many") {
              this.dongtai = res.data.data;
            } else {
              this.jingtai = res.data.data;
            }

            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
          } else {
            this.$message({
              message: res.data.meta.msg,
              type: "error",
            });
          }
        });
      }
    },
    //级联选择器
    handleChange() {
      this.getNumData();
    },

    //tab
    handleClick() {
      console.log(this.activeNam);
      this.getNumData();
    },

    //添加
    //清空输入框
    tianjiacanshu() {
      this.$refs.canshulist.resetFields();
    },

    addcanshu() {
      this.$refs.canshulist.validate((valid) => {
        if (!valid) return;
        AddNumList(
          this.cateId,
          this.canshulist.attr_name,
          this.activeName
        ).then((res) => {
          if (res.data.meta.status == 201) {
            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
            this.getNumData();
          } else {
            this.$message({
              message: res.data.meta.msg,
              type: "error",
            });
          }
        });
      });
      this.dialogVisible = false;
    },

    //修改
    //清空输入框
    xiugaicanshu() {
      this.$refs.editlist.resetFields();
    },
    xiugainum(row) {
      this.editdialogVisible = true;
      this.editlist.attr_name = row.attr_name;
      this.editlist.attr_id = row.attr_id;
    },

    sureEdit() {
      this.$refs.editlist.validate((valid) => {
        if (!valid) return;
        gengxinNum(
          this.cateId,
          this.editlist.attr_id,
          this.editlist.attr_name,
          this.activeName
        ).then((res) => {
          if (res.data.meta.status == 200) {
            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
            this.getNumData();
          } else {
            this.$message({
              message: res.data.meta.msg,
              type: "error",
            });
          }
        });
      });
      this.editdialogVisible = false;
    },

    //删除
    shanchuNum(row) {
      DeleteNum(this.cateId, row.attr_id).then((res) => {
        if (res.data.meta.status == 200) {
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
          this.getNumData();
        } else {
          this.$message({
            message: res.data.meta.msg,
            type: "error",
          });
        }
      });
    },
    

 //标签
    saveVals(row){
        gengxinNum(
          this.cateId,
          row.attr_id,
          row.attr_name,
          row.attr_sel,
          row.attr_vals.join(',')
        ).then((res) => {
          if (res.data.meta.status == 200) {
            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
            // this.getNumData();
          } else {
            this.$message({
              message: res.data.meta.msg,
              type: "error",
            });
          }
        });
    },
   
    showInput(row) {
        row.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },
    //添加标签
      handleInputConfirm(row) {
       
        if (row.inputValue.trim().length === 0) {
            row.inputVisible = false;
            row.inputValue = '';
            return
         
        }
        row.attr_vals.push(row.inputValue.trim());
        row.inputVisible = false;
        row.inputValue = '';
        this.saveVals(row)
      },

    //删除标签
    handelclose(row,i){
        console.log(row.id,i)
        row.attr_vals.splice(i,1)
        this.saveVals(row)
    }
  },
};
</script>

<style scoped lang="less">
.cat {
  margin: 15px 0;
}
.xuanze {
  margin-right: 15px;
}
.btn {
  padding: 0;
  margin: 0px 0 15px;
}
.btn3 {
  width: 64px;
  height: 24px;
  font-size: 12px;
  padding: 0;
}
.button-new-tag{
    width: 68px;
    padding: 0;
    margin-left: 10px;
}
.input-new-tag{
    width: 15%;
    margin-left: 10px;
}
.tagbox{
    margin: 0  10px;
}
</style>